﻿Imports System
Imports System.IO
Imports System.Net.Mail

''' <summary>
''' Sends event mail to administrators.
''' </summary>
''' <remarks>Mail settings are set up through the application settings.</remarks>
Public Class NotifyMail
    'TODO: This could be more robust, but it suffices for now.

    Public Sub SendServiceMail(ByVal strSubject As String, ByVal strMessage As String)

        Try
            'Make sure that the mail setting is enabled.
            If My.Settings.MailEnabled Then

                'Create a SmtpClient object to handle the mail message
                Dim client As New SmtpClient(My.Settings.MailServer)

                'Set the client port
                client.Port = My.Settings.MailPort

                'Set the security
                client.EnableSsl = My.Settings.MailSSL

                'Establish credentials
                If My.Settings.MailAuthenticate Then
                    Dim creds As New System.Net.NetworkCredential(My.Settings.MailUserName, My.Settings.MailPassword)
                    client.Credentials = creds
                End If

                'Create a MailMessage object
                Dim msg As New MailMessage(My.Settings.ServiceEmail, My.Settings.MailRecipients)

                'Set the message subject
                msg.Subject = strSubject

                'Set the message body
                msg.Body = strMessage

                'Send the message
                client.Send(msg)

                'Clean up
                client = Nothing
                msg = Nothing
            End If
        Catch ex As Exception
            System.Diagnostics.EventLog.WriteEntry("VOEIS Hub Push Service", "Error sending mail: " & Environment.NewLine & ex.Message.ToString & Environment.NewLine & "Check your mail settings via the service management client interface.", EventLogEntryType.Error)
        End Try
    End Sub
End Class
